package cn.yqn.org.controller;

import cn.yqn.basic.annotation.PreAuthorize;
import cn.yqn.basic.utils.JsonResult;
import cn.yqn.basic.utils.PageList;
import cn.yqn.org.domain.Department;
import cn.yqn.org.query.DepartmentQuery;
import cn.yqn.org.service.IDepartmentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /**
    * @Title: 查询全部
    * @Description: TODO
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/7/19 13:00
    * @Parameters: []
    * @Return java.util.List<cn.yqn.org.domain.Department>
    */
    @GetMapping
    @PreAuthorize(name = "所有部门",sn = "dept:get")
    public List<Department> loadAll() {
        return departmentService.loadAll();
    }

    /**
    * @Title: 根据id查询
    * @Description: TODO
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/7/19 13:00
    * @Parameters: [id]
    * @Return cn.yqn.org.domain.Department
    */
    @GetMapping("/{id}")
    @PreAuthorize(name = "根据id部门",sn = "dept:get")
    public Department loadById(@PathVariable("id") Long id) {
        return departmentService.loadById(id);
    }

    /**
    * @Title: 根据id删除
    * @Description: TODO
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/7/19 13:00
    * @Parameters: [id]
    * @Return cn.yqn.basic.util.JsonResult
    */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除部门",sn = "dept:delete")
    public JsonResult remove(@PathVariable("id") Long id) {
        try {
            departmentService.remove(id);
            return JsonResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /** 
    * @Title: 批量删除
    * @Description: TODO
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/7/19 13:03  
    * @Parameters: [ids]
    * @Return cn.yqn.basic.util.JsonResult
    */
    @PatchMapping
    @PreAuthorize(name = "部门批量删除",sn = "dept:patch")
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            departmentService.patchDel(ids);
            return JsonResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
    * @Title: 新增或修改
    * @Description: TODO
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/7/19 13:00
    * @Parameters: [department]
    * @Return cn.yqn.basic.util.JsonResult
    */
    @PutMapping
    @PreAuthorize(name = "保存部门",sn = "dept:put")
    public JsonResult saveOrUpdate(@RequestBody Department department) {
        try {
            if (department.getId() == null) {
                departmentService.save(department);
            } else{
                departmentService.update(department);
            }
            return JsonResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
    * @Title: 分页展示
    * @Description: TODO
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/7/19 13:00
    * @Parameters: [departmentQuery]
    * @Return cn.yqn.basic.util.PageList<cn.yqn.org.domain.Department>
    */
    @PostMapping
    @PreAuthorize(name = "部门分页信息",sn = "dept:post")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery) {
        try {
            return departmentService.pageQuery(departmentQuery);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<Department>();
        }
    }

    @GetMapping("/tree")
    @PreAuthorize(name = "部门树",sn = "dept:get")
    public List<Department> Tree() {
        return departmentService.Tree();
    }

}
